package net.huashitong.Project.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.Project.entity.EarlyWarningNode;
import net.huashitong.Project.filter.EarlyWarningNodeFilter;
import net.huashitong.user.filter.BnpUserFilter;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class EarlyWarningNodeDao extends StringDao<EarlyWarningNode> {

    public EarlyWarningNode queryEarlyWarningNodeByNodeSysId(String nodeSysId) {
        GenericQuery query = createQuery("obj");
        query.append(" and obj.nodeSysId=:nodeSysId").setParam("nodeSysId", nodeSysId);
        query.append(" order by obj.createDate DESC ");
        return query.firstResult();
    }

    public List<EarlyWarningNode> queryEarlyWarningNodeListByStatus() {
        GenericQuery query = createQuery("obj");
        query.append(" and obj.status >=:status").setParam("status", "0");
        query.append(" order by obj.createDate DESC ");
        return query.listResult();
    }
    public List<EarlyWarningNode> queryEarlyWarningNodeList(){
        GenericQuery query = createQuery("obj");
        query.append(" order by cast(obj.state as int) ASC ");
        return query.listResult();
    }
    public List<EarlyWarningNode> queryEarlyWarningNodeListByUserId(BnpUserFilter filter) {
        GenericQuery query = createQuery("obj");
        query.append(" and obj.operatorPhone=:operatorPhone").setParam("operatorPhone", filter.getPhone());
        query.append(" and obj.state=:state").setParam("state", "0");
        query.append(" order by obj.createDate DESC ");
        return query.listResult(filter);
    }

    public List<EarlyWarningNode> queryEarlyWarning(EarlyWarningNodeFilter filter) {
        GenericQuery query = createQuery("obj");
        if (filter.getProjectId() != null && !"".equals(filter.getProjectId()))
            query.append(" and obj.projectId=:projectId").setParam("projectId", filter.getProjectId());
        if (filter.getProjectName() != null && !"".equals(filter.getProjectName()))
            query.append(" and obj.projectName like:projectName").setParam("projectName", "%" + filter.getProjectName() + "%");
        if (filter.getNodeSysId() != null && !"".equals(filter.getNodeSysId()))
            query.append(" and obj.nodeSysId =:nodeSysId").setParam("nodeSysId", filter.getNodeSysId());
        if (filter.getNodeId() != null && !"".equals(filter.getNodeId()))
            query.append(" and obj.nodeId =:nodeId").setParam("nodeId", filter.getNodeId());
        if (filter.getNodeName() != null && !"".equals(filter.getNodeName()))
            query.append(" and obj.nodeName =:nodeName").setParam("nodeName", filter.getNodeName());
        if (filter.getResDept() != null && !"".equals(filter.getResDept()))
            query.append(" and obj.resDept =:resDept").setParam("resDept", filter.getResDept());
        if (filter.getResUser() != null && !"".equals(filter.getResUser()))
            query.append(" and obj.resUser =:resUser").setParam("resUser", filter.getResUser());
        if (filter.getResUserPhone() != null && !"".equals(filter.getResUserPhone()))
            query.append(" and obj.resUserPhone =:resUserPhone").setParam("resUserPhone", filter.getResUserPhone());
        if (filter.getOperatorUser() != null && !"".equals(filter.getOperatorUser()))
            query.append(" and obj.operatorUser =:operatorUser").setParam("operatorUser", filter.getOperatorUser());
        if (filter.getOperatorPhone() != null && !"".equals(filter.getOperatorPhone()))
            query.append(" and obj.operatorPhone =:operatorPhone").setParam("operatorPhone", filter.getOperatorPhone());
        if (filter.getNodeType() != null && !"".equals(filter.getNodeType()))
            query.append(" and obj.nodeType =:nodeType").setParam("nodeType", filter.getNodeType());
        if (filter.getPlanTime() != null && !"".equals(filter.getPlanTime()))
            query.append(" and obj.planTime =:planTime").setParam("planTime", filter.getPlanTime());
        if (filter.getFactTime() != null && !"".equals(filter.getFactTime()))
            query.append(" and obj.factTime =:factTime").setParam("factTime", filter.getFactTime());
        if (filter.getPlanBeginDate() != null && !"".equals(filter.getPlanBeginDate()))
            query.append(" and obj.planBeginDate =:planBeginDate").setParam("planBeginDate", filter.getPlanBeginDate());
        if (filter.getFactFinishDate() != null && !"".equals(filter.getFactFinishDate()))
            query.append(" and obj.factFinishDate =:factFinishDate").setParam("factFinishDate", filter.getFactFinishDate());
        if (filter.getStatus() != null && !"".equals(filter.getStatus()))
            query.append(" and obj.status =:status").setParam("status", filter.getStatus());
        if (filter.getState() != null && !"".equals(filter.getState()))
            query.append(" and obj.state =:state").setParam("state", filter.getState());
        if (filter.getNumber() != null && !"".equals(filter.getNumber()))
            query.append(" and obj.number =:number").setParam("number", filter.getNumber());
        if(filter.getStartTime()!= null && !"".equals(filter.getStartTime()))
            query.append(" and obj.createDate >=:startTime").setParam("startTime", filter.getStartTime());
        if(filter.getEndTime()!= null && !"".equals(filter.getEndTime()))
            query.append(" and obj.createDate <=:endTime").setParam("endTime", filter.getEndTime());
        if(filter.getProjectIds()!=null && !"".equals(filter.getProjectIds()))
            query.append(" and project_id in("+filter.getProjectIds()+")");
        query.append(" order by obj.createDate DESC ");

        System.out.println("count1="+query.listResult().size());
        return query.listResult(filter);
    }

    public int querywarningCount(EarlyWarningNodeFilter filter){
        GenericQuery query = createQuery("obj");
        if (filter.getProjectId() != null && !"".equals(filter.getProjectId()))
            query.append(" and obj.projectId=:projectId").setParam("projectId", filter.getProjectId());
        if (filter.getProjectName() != null && !"".equals(filter.getProjectName()))
            query.append(" and obj.projectName like:projectName").setParam("projectName", "%" + filter.getProjectName() + "%");
        if (filter.getNodeSysId() != null && !"".equals(filter.getNodeSysId()))
            query.append(" and obj.nodeSysId =:nodeSysId").setParam("nodeSysId", filter.getNodeSysId());
        if (filter.getNodeId() != null && !"".equals(filter.getNodeId()))
            query.append(" and obj.nodeId =:nodeId").setParam("nodeId", filter.getNodeId());
        if (filter.getNodeName() != null && !"".equals(filter.getNodeName()))
            query.append(" and obj.nodeName =:nodeName").setParam("nodeName", filter.getNodeName());
        if (filter.getResDept() != null && !"".equals(filter.getResDept()))
            query.append(" and obj.resDept =:resDept").setParam("resDept", filter.getResDept());
        if (filter.getResUser() != null && !"".equals(filter.getResUser()))
            query.append(" and obj.resUser =:resUser").setParam("resUser", filter.getResUser());
        if (filter.getResUserPhone() != null && !"".equals(filter.getResUserPhone()))
            query.append(" and obj.resUserPhone =:resUserPhone").setParam("resUserPhone", filter.getResUserPhone());
        if (filter.getOperatorUser() != null && !"".equals(filter.getOperatorUser()))
            query.append(" and obj.operatorUser =:operatorUser").setParam("operatorUser", filter.getOperatorUser());
        if (filter.getOperatorPhone() != null && !"".equals(filter.getOperatorPhone()))
            query.append(" and obj.operatorPhone =:operatorPhone").setParam("operatorPhone", filter.getOperatorPhone());
        if (filter.getNodeType() != null && !"".equals(filter.getNodeType()))
            query.append(" and obj.nodeType =:nodeType").setParam("nodeType", filter.getNodeType());
        if (filter.getPlanTime() != null && !"".equals(filter.getPlanTime()))
            query.append(" and obj.planTime =:planTime").setParam("planTime", filter.getPlanTime());
        if (filter.getFactTime() != null && !"".equals(filter.getFactTime()))
            query.append(" and obj.factTime =:factTime").setParam("factTime", filter.getFactTime());
        if (filter.getPlanBeginDate() != null && !"".equals(filter.getPlanBeginDate()))
            query.append(" and obj.planBeginDate =:planBeginDate").setParam("planBeginDate", filter.getPlanBeginDate());
        if (filter.getFactFinishDate() != null && !"".equals(filter.getFactFinishDate()))
            query.append(" and obj.factFinishDate =:factFinishDate").setParam("factFinishDate", filter.getFactFinishDate());
        if (filter.getStatus() != null && !"".equals(filter.getStatus()))
            query.append(" and obj.status =:status").setParam("status", filter.getStatus());
        if (filter.getState() != null && !"".equals(filter.getState()))
            query.append(" and obj.state =:state").setParam("state", filter.getState());
        if (filter.getNumber() != null && !"".equals(filter.getNumber()))
            query.append(" and obj.number =:number").setParam("number", filter.getNumber());
        if(filter.getStartTime()!= null && !"".equals(filter.getStartTime()))
            query.append(" and obj.createDate >=:startTime").setParam("startTime", filter.getStartTime());
        if(filter.getEndTime()!= null && !"".equals(filter.getEndTime()))
            query.append(" and obj.createDate <=:endTime").setParam("endTime", filter.getEndTime());
        if(filter.getProjectIds()!=null && !"".equals(filter.getProjectIds()))
            query.append(" and project_id in("+filter.getProjectIds()+")");
        System.out.println("count="+query.count());
        return query.count();
    }

    /**
     * 查询某部门的预警信息处理与否的数量
     *
     * @param resDept
     * @param state
     * @return
     */
    public int queryEarlyWarningStateByDept(String resDept, String state) {
        GenericQuery query = createQuery("obj");
        if (ObjectUtils.isNotBlank(resDept)) {
            query.append(" and obj.resDept =:resDept").setParam("resDept", resDept);
        } else {
            query.append(" and res_dept not in('自然资源局', '住建局', '发改局', '生态环境局', '水利局', '林业局', '财政局', '气象局', '消防中队', '人防办', '人社局')");
        }
        if (ObjectUtils.isNotBlank(state)) {
            query.append(" and obj.state =:state").setParam("state", state);
        }
        return query.count();
    }

    public List<EarlyWarningNode> getAllEarlyWarningNodeNoDealWith(){
        GenericQuery query = createQuery("obj");
        query.append(" and obj.state =:state").setParam("state", "0");
        return query.listResult();
    }

    public List<EarlyWarningNode> getEarlyWarningByProjectIdAndNodeId(String projectId, String nodeId){
        GenericQuery query = createQuery("obj");
        query.append(" and obj.projectId =:projectId").setParam("projectId", projectId);
        query.append(" and obj.nodeId =:nodeId").setParam("nodeId", nodeId);
        return query.listResult();
    }
}
